/*     */ package com.langsotech.ide.mozilla.browser.core;
/*     */ 
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.net.URL;
/*     */ import java.util.Dictionary;
/*     */ import org.eclipse.core.runtime.FileLocator;
/*     */ import org.eclipse.core.runtime.ILog;
/*     */ import org.eclipse.core.runtime.IPath;
/*     */ import org.eclipse.core.runtime.IStatus;
/*     */ import org.eclipse.core.runtime.Platform;
/*     */ import org.eclipse.core.runtime.Plugin;
/*     */ import org.eclipse.core.runtime.Status;
/*     */ import org.osgi.framework.Bundle;
/*     */ import org.osgi.framework.BundleContext;
/*     */ 
/*     */ public class MozillaCorePlugin extends Plugin
/*     */ {
/*     */   public static final String PLUGIN_ID = "org.nazca.ide.mozilla.browser.core";
/*     */   public static final String ATF_INTERNAL = "___ATF_INTERNAL";
/*     */   public static final boolean DEBUG_BROWSERSTART = false;
/*  30 */   private static final String XULRUNNER_BUNDLE = getXulrunnerBundleName();
/*     */   private static final String XULRUNNER_ENTRY = "/xulrunner";
/*     */   private static MozillaCorePlugin plugin;
/*     */ 
/*     */   public MozillaCorePlugin()
/*     */   {
/*  36 */     plugin = this;
/*     */   }
/*     */ 
/*     */   public void start(BundleContext context) throws Exception {
/*  40 */     super.start(context);
/*     */   }
/*     */ 
/*     */   public void stop(BundleContext context) throws Exception {
/*  44 */     super.stop(context);
/*  45 */     plugin = null;
/*     */   }
/*     */ 
/*     */   public static MozillaCorePlugin getDefault() {
/*  49 */     return plugin;
/*     */   }
/*     */ 
/*     */   public static String getXulRunnerBundleName()
/*     */   {
/*  54 */     return XULRUNNER_BUNDLE;
/*     */   }
/*     */ 
/*     */   public static XulBundleInfo getXulRunnerBundleInfo()
/*     */     throws Exception
/*     */   {
/*  61 */     XulBundleInfo info = new XulBundleInfo();
/*     */ 
/*  63 */     Bundle xulRunnerBundle = Platform.getBundle(getXulRunnerBundleName());
/*  64 */     if (xulRunnerBundle == null) {
/*  65 */       logInfo("Bundle " + getXulRunnerBundleName() + " is not found.");
/*     */     }
/*     */ 
/*  69 */     String version = (String)xulRunnerBundle.getHeaders().get("Bundle-Version");
/*  70 */     logInfo("xulRunnerVersion is:" + version);
/*     */ 
/*  74 */     File xulRunnerDir = null;
/*     */ 
/*  76 */     URL url = xulRunnerBundle.getEntry("/xulrunner");
/*  77 */     if (url == null)
/*  78 */       logInfo("Bundle " + getXulRunnerBundleName() + 
/*  79 */         " doesn't contain " + "/xulrunner");
/*     */     try
/*     */     {
/*  82 */       xulRunnerDir = new File(FileLocator.toFileURL(FileLocator.resolve(url)).getFile());
/*  83 */       if ((xulRunnerDir != null) && (!(xulRunnerDir.isDirectory()))) {
/*  84 */         xulRunnerDir = xulRunnerDir.getParentFile();
/*     */       }
/*     */     }
/*     */     catch (IOException ioe)
/*     */     {
/*  89 */       logInfo("Cannot get path to XULRunner from bundle " + 
/*  90 */         getXulRunnerBundleName());
/*  91 */       throw new Exception(
/*  92 */         "Cannot get path to XULRunner from bundle " + 
/*  93 */         getXulRunnerBundleName(), ioe);
/*     */     }
/*     */ 
/*  96 */     info.setName(getXulRunnerBundleName());
/*  97 */     info.setVersion(version);
/*  98 */     if (xulRunnerDir != null) {
/*  99 */       xulRunnerDir = xulRunnerDir.getCanonicalFile();
/*     */     }
/* 101 */     info.setXulRunnerDir(xulRunnerDir);
/*     */ 
/* 103 */     return info;
/*     */   }
/*     */ 
/*     */   protected static String getXulrunnerBundleName()
/*     */   {
/* 110 */     return "org.mozilla.xulrunner." + Platform.getWS() + "." + Platform.getOS() + 
/* 111 */       (("macosx".equals(Platform.getOS())) ? "" : new StringBuilder(".").append(Platform.getOSArch()).toString());
/*     */   }
/*     */ 
/*     */   public static File getUserDataPath()
/*     */   {
/* 119 */     return Platform.getLocation().append(".metadata/.plugins").append("org.nazca.ide.mozilla.browser.core").append("xulrunner").toFile();
/*     */   }
/*     */ 
/*     */   public static void logInfo(String message) {
/* 123 */     log(1, message, null);
/*     */   }
/*     */ 
/*     */   public static void logWarning(String message) {
/* 127 */     log(2, message, null);
/*     */   }
/*     */ 
/*     */   public static void logWarning(String message, Throwable e) {
/* 131 */     log(2, message, e);
/*     */   }
/*     */ 
/*     */   public static void logError(String message) {
/* 135 */     logError(message, null);
/*     */   }
/*     */ 
/*     */   public static void logError(String message, Throwable e) {
/* 139 */     log(4, message, e);
/*     */   }
/*     */ 
/*     */   public static void log(int level, String message, Throwable e) {
/* 143 */     if (message == null) {
/* 144 */       message = "";
/*     */     }
/* 146 */     Status status = new Status(level, "org.eclipse.atf.mozilla.ide.core", 
/* 147 */       0, message, e);
/* 148 */     logStatus(status);
/*     */   }
/*     */ 
/*     */   public static synchronized void logStatus(IStatus status) {
/*     */     try {
/* 153 */       getDefault().getLog().log(status);
/*     */     } catch (Exception localException) {
/* 155 */       System.out.println(status.getMessage());
/*     */     }
/*     */   }
/*     */ }
